<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - loaders - 3DS loader</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<link type="text/css" rel="stylesheet" href="main.css">
	</head>

	<body>
		<div id="info">
			<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - 3DS loader
		</div>

		<!-- Import maps polyfill -->
		<!-- Remove this when import maps will be widely supported -->
		<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>

		<script type="importmap">
			{
				"imports": {
					"three": "../build/three.module.js",
					"three/addons/": "./jsm/"
				}
			}
		</script>

		<script type="module">

			import * as THREE from 'three';

			import { TrackballControls } from 'three/addons/controls/TrackballControls.js';
			import { TDSLoader } from 'three/addons/loaders/TDSLoader.js';

			let container, controls;
			let camera, scene, renderer;

			init();
			animate();

			function init() {

				container = document.createElement( 'div' );
				document.body.appendChild( container );

				camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 10 );
				camera.position.z = 2;

				scene = new THREE.Scene();
				scene.add( new THREE.HemisphereLight() );

				const directionalLight = new THREE.DirectionalLight( 0xffeedd );
				directionalLight.position.set( 0, 0, 2 );
				scene.add( directionalLight );

				//3ds files dont store normal maps
				const normal = new THREE.TextureLoader().load( 'models/3ds/portalgun/textures/normal.jpg' );

				const loader = new TDSLoader( );
				loader.setResourcePath( 'models/3ds/portalgun/textures/' );
				loader.load( 'models/3ds/portalgun/portalgun.3ds', function ( object ) {

					object.traverse( function ( child ) {

						if ( child.isMesh ) {

							child.material.specular.setScalar( 0.1 );
							child.material.normalMap = normal;

						}

					} );

					scene.add( object );

				} );

				renderer = new THREE.WebGLRenderer();
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.outputEncoding = THREE.sRGBEncoding;
				container.appendChild( renderer.domElement );

				controls = new TrackballControls( camera, renderer.domElement );

				window.addEventListener( 'resize', resize );

			}

			function resize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			function animate() {

				controls.update();
				renderer.render( scene, camera );

				requestAnimationFrame( animate );

			}
		</script>

	</body>
</html>
